//
//  HFApplicationDelegate.m
//  HocusFocus
//
//  Created by Juan Carlos Jimenez on 4/6/08.
//  Copyright 2008 Juan Carlos Jimenez. All rights reserved.
//

#import "HFApplicationDelegate.h"
#import "HFCaptureWindowController.h"


@implementation HFApplicationDelegate

- (void)dealloc
{
    [statusItem release];
    [super dealloc];
}

- (void)awakeFromNib
{
	NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
	if (![[infoDictionary objectForKey:@"NSUIElement"] isEqualToString:@"1"] &&
		![[infoDictionary objectForKey:@"LSUIElement"] isEqualToString:@"1"]) {
		return;
	}
	if (statusItemMenu == nil) {
		NSLog(@"WARNING: Status menu is not set.");
		return;
	}
	
	NSSize scaledSize = NSMakeSize(17.0, 17.0);
	NSSize appImageSize;
	NSImage *appImage = [[NSApplication sharedApplication] applicationIconImage];
	NSImage *scaledImage = [[[NSImage alloc] initWithSize:scaledSize] autorelease];
	
	[scaledImage lockFocus];
	appImageSize = [appImage size];
	[appImage drawInRect:NSMakeRect(0.0, 0.0, scaledSize.width, scaledSize.height)
				fromRect:NSMakeRect(0.0, 0.0, appImageSize.width, appImageSize.height)
			   operation:NSCompositeSourceOver
				fraction:1.0];
	[scaledImage unlockFocus];
	
	statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
	[statusItem setImage:scaledImage];
	[statusItem setMenu:statusItemMenu];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuItemWillSendAction:) name:NSMenuWillSendActionNotification object:statusItemMenu];
}

- (void)applicationDidResignActive:(NSNotification *)notification
{
	[captureWindowController cancelOperation:self];
}

- (void)menuItemWillSendAction:(NSNotification *)notification
{
	[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}

@end
